
var Appid = 'xxxxxxxxxxxxxx';                   //appid
var AppidFlag = 'xcx';
var Secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';    //Secret

var apiUpLoad = 'https://www.uhuijia.com.cn/';      //上传地址
var apiHost = 'http://47.92.67.233:8080/mall'; //外部接口
// var apiHost = 'http://develop.jhjvip.cn:8080/mall'; //外部接口
// var apiHost = 'http://192.168.1.110:8080/mall'; //外部接口
// var apiHost = 'https://www.uhuijia.com.cn/mall'; //正式库

var token = wx.getStorageSync('token');


App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    wx.setStorageSync('apiHost', apiHost);
    wx.setStorageSync('apiUpLoad', apiUpLoad);

    wx.setStorageSync('Appid', Appid);
    wx.setStorageSync('Secret', Secret);

    wx.setStorageSync('AppidFlag', AppidFlag);

    // if (token){
    //   checkToken(token)
    // }else{
    //   userLogin();
    //   toWelCome();
    // }

    /**
     * 用户登录
     */
    function userLogin() {
      // 登录
      wx.login({
        success: function (r) {
          var code = r.code;//登录凭证
          if (code) {
            getSessionKey(code);
          } else {
            console.log('获取用户登录态失败！' + r.errMsg)
          }
        },
        fail: function () {
          console.log('登陆失败')
        }
      })
    }

    /**
   * 获取用户信息
   */
    function getUseInfo(code) {
      var that = this;
      //2、调用获取用户信息接口
      wx.getUserInfo({
        success: function (res) {
          VERBurl(res, code)   //解密用户登录完成后的url
        },
        fail: function (res) {
          console.log('获取用户信息失败')
        }
      })
    }

    //跳转欢迎页
    function toWelCome() {
      // 加载中
      wx.showLoading({
        title: '加载中',
        icon: 'loading',
      });

      wx.redirectTo({
        url: '/pages/welCome/welCome',
        success: function () {
          wx.hideLoading();   //隐藏加载loading
        },
      });
    }

    /**
     * 校验token信息
     */
    function checkToken(token) {
      wx.request({
        url: apiHost + "/mobile/login/in",//自己的服务接口地址
        method: 'post',
        data: {
          appId: AppidFlag,
          token: token
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          var code = res.data.code;
          var msg = res.data.msg;
          if (code == "10000") {
            var phone = res.data.data.phone;
            if (phone) {
              wx.setStorageSync('phone', phone);
              wx.switchTab({
                url: '/pages/index/index',
                success: function () {
                  wx.hideLoading();   //隐藏加载loading
                },
              });
            } else {
              userLogin();
            }
          } else {
            wx.showToast({
              title: msg,
              icon: 'none',
              duration: 5000,
              mask: true
            });
          }
        },
        fail: function (e) {
          wx.hideLoading();   //隐藏加载loading
          wx.showToast({
            title: '网络异常',
            icon: 'none',
            duration: 5000,
            mask: true
          });
        }
      })
    }

    /**
     * 通过code获取 unionId
     */
    function getSessionKey(code) {
      //3.请求自己的服务器，解密用户信息 获取unionId等加密信息  (注意：这个东西必须需要 开放平台 授权)
      //https://api.weixin.qq.com/sns/jscode2session?appid=" + Appid + "&secret=" + Secret+"&js_code=" + code+"&grant_type=authorization_code
      wx.request({
        url: apiHost + "/mobile/xcx/getOpenidUnionidByCode",//自己的服务接口地址
        method: 'GET',
        data: {
          appId: Appid,
          secret: Secret,
          code: code
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {

          var code = res.data.code;
          var msg = res.data.msg;
          if (code == "10000") {

            var openid = res.data.data.openid;
            var session_key = res.data.data.session_key;
            var unionid = res.data.data.unionid;

            if (unionid == '') {
              getUseInfo(code);
            }

            wx.setStorageSync('openid', openid);
            wx.setStorageSync('session_key', session_key);
            wx.setStorageSync('unionid', unionid);

          } else {
            wx.showToast({
              title: msg,
              icon: 'none',
              duration: 5000,
              mask: true
            });
          }
        },
        fail: function (e) {
          wx.hideLoading();   //隐藏加载loading
          wx.showToast({
            title: '网络异常',
            icon: 'none',
            duration: 5000,
            mask: true
          });
        }
      })
    }
  },
  globalData: {
    userInfo: null
  }
})